﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using static Acme.BookStore.Permissions.BookStorePermissions;

namespace Acme.BookStore.Car
{
    public class CarInventryAppService :
        CrudAppService<
            CarInventryss,
            CarInventryDto,
            int,
            PagedAndSortedResultRequestDto,
            CreateUpdateCarInventryDto>,
        ICarInventryAppService
    {
        private readonly IRepository<Fleetcar, int> _fleetcarRepository;
        private readonly IRepository<Vehicle, Guid> _vehicleRepository;
        public CarInventryAppService(IRepository<CarInventryss, int> repository,
            IRepository<Fleetcar, int> fleetcarRepository,
            IRepository<Vehicle, Guid> vehicleRepository)
            : base(repository)
        {
            _fleetcarRepository = fleetcarRepository;
            _vehicleRepository = vehicleRepository;
        }
        public async Task<CarInventryDto> AssignVehicleAsync(int fleetcarId, string vehicleNumber,Guid vehileid)
        {
            // 1. 获取派车管理数据
            var fleetcar = await _fleetcarRepository.GetAsync(fleetcarId);
            if (fleetcar == null)
            {
                throw new Exception("未找到派车记录");
            }

            // 获取派车管理信息
            var vehicle = await _vehicleRepository.FirstOrDefaultAsync(v => v.VehicleNumber == vehicleNumber);
            if (vehicle == null)
            {
                throw new UserFriendlyException("未找到车辆信息");
            }

            // 2. 创建派车详细记录
            var carInventry = new CarInventryss
            {
                FleetcarId = fleetcar.Id,
                VehicleId = vehicle.Id,
                VehicleNumber = vehicle.VehicleNumber,
                Shipper = fleetcar.Shipper,
                ShipperAddress = fleetcar.ShipperAddress,
                Consignee = fleetcar.Consignee,
                ConsigneeAddress = fleetcar.ConsigneeAddress,
                Goods = fleetcar.Goods,
                PieceCount = fleetcar.PieceCount,
                Declaration = fleetcar.Declaration,
                CubicMeterss = fleetcar.CubicMeterss,
                Weightk = fleetcar.Weightk,
                Lengthm = fleetcar.Lengthm,
                Widthm = fleetcar.Widthm,
                Heightm = fleetcar.Heightm,
                FreightAmount = fleetcar.FreightAmount,
                ReturnRequired = fleetcar.ReturnRequired,
                CustomerNo = fleetcar.CustomerNo,
                OrderNo = fleetcar.OrderNo,
                Remarks = fleetcar.Remarks
            };

            // 3. 保存新记录
            await Repository.InsertAsync(carInventry);

            // 4. 删除原派车管理记录
            await _fleetcarRepository.DeleteAsync(fleetcarId);

            // 5. 返回创建的派车详细记录
            return ObjectMapper.Map<CarInventryss, CarInventryDto>(carInventry);
        }
    }
}